package com.madi.company.ui.mainfragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jaydenxiao.common.base.BaseFragment;
import com.madi.company.R;
import com.madi.company.ui.main.activity.JobTypeActivity;
import com.madi.company.ui.main.activity.ProfessionActivity;
import com.madi.company.ui.main.activity.SearchResumeActivity;
import com.madi.company.utils.SerializableMap;

import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * des:首頁
 * Created by 叮咛
 * on 2016.11.29
 */
public class HomeFragment extends BaseFragment {


    @Bind(R.id.job_type_TextView)
    TextView mJobTypeTextView;
    @Bind(R.id.search_position_btn)
    TextView mSearchPosiontionBtn;
    @Bind(R.id.profession_type_TV)
    TextView mProfessionTextView;
    @Bind(R.id.profession_type_ll)
    LinearLayout mprofessionType;
    @Bind(R.id.job_type_ll)
    LinearLayout mJobTypeLl;
    @Bind(R.id.job_address_ll)
    LinearLayout mJobAddressLl;

    private StringBuffer mProfessionShowText;
    private StringBuffer mProfessionSearchId;
    private SerializableMap mProfessionSelectedMap;
    private Intent mIntent;
    private Bundle mBundle;
    private StringBuffer mJobTypeShowText;
    private StringBuffer mJobTypeSearchId;
    private SerializableMap mJobTypeSelectedMap;

    @Override
    protected int getLayoutResource() {
        return R.layout.fra_home;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    protected void initView() {
        mProfessionSearchId = new StringBuffer();
        mJobTypeSearchId = new StringBuffer();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && data != null) {
            Bundle bundle = data.getExtras();
            mProfessionSelectedMap = (SerializableMap) bundle.get("map");
            Map<String, Integer> map = mProfessionSelectedMap.getMap();
            if (map != null) {
                mProfessionShowText = new StringBuffer();
                for (Map.Entry entry : map.entrySet()) {
                    String key = (String) entry.getKey();
                    mProfessionShowText.append(key + ",");
                    Integer integer = map.get(key);
                    mProfessionSearchId.append(integer);
                }
                if (mProfessionShowText.toString().length() > 0) {
                    mProfessionTextView.setText(mProfessionShowText.toString().substring(0, mProfessionShowText.toString().length() - 1));
                }
            } else {
                mProfessionTextView.setText("");
                mProfessionTextView.setHint(getResources().getString(R.string.search_optional_number));
            }
        } else if (requestCode == 200 && data != null) {
            Bundle bundle = data.getExtras();
            mJobTypeSelectedMap = (SerializableMap) bundle.get("map");
            Map<String, Integer> map = mJobTypeSelectedMap.getMap();
            if (map != null) {
                mJobTypeShowText = new StringBuffer();
                for (Map.Entry entry : map.entrySet()) {
                    String key = (String) entry.getKey();
                    mJobTypeShowText.append(key + ",");
                    Integer integer = map.get(key);
                    mJobTypeSearchId.append(integer);
                }
                if (mJobTypeShowText.toString().length() > 0) {
                    mJobTypeTextView.setText(mJobTypeShowText.toString().substring(0, mJobTypeShowText.toString().length() - 1));
                }
            } else {
                mJobTypeTextView.setText("");
                mJobTypeTextView.setHint(getResources().getString(R.string.search_optional_number));
            }
        }
    }

    @OnClick({R.id.job_type_ll, R.id.profession_type_ll, R.id.search_position_btn, R.id.job_address_ll})
    public void onClick(View view) {
        mIntent = new Intent();
        mBundle = new Bundle();
        switch (view.getId()) {
            case R.id.job_type_ll:// 职位类别
                String typeTitle = getString(R.string.search_position);
                mIntent.setClass(getActivity(), JobTypeActivity.class);
                mBundle.putSerializable("map", mJobTypeSelectedMap);
                mBundle.putString("typeTitleName", typeTitle);
                // 标题名字
                mIntent.putExtras(mBundle);
                startActivityForResult(mIntent, 200);
                getActivity().overridePendingTransition(R.anim.fade_in,
                        com.jaydenxiao.common.R.anim.fade_out);
                break;
            case R.id.profession_type_ll: // 行业类别
                String professionTitle = getString(R.string.search_trade);
                mIntent.setClass(getActivity(), ProfessionActivity.class);
                mBundle.putSerializable("map", mProfessionSelectedMap);
                // 标题名字
                mBundle.putString("professionTitleName", professionTitle);
                mIntent.putExtras(mBundle);
                startActivityForResult(mIntent, 100);
                getActivity().overridePendingTransition(R.anim.fade_in,
                        com.jaydenxiao.common.R.anim.fade_out);
                break;
            case R.id.search_position_btn:// 开始搜索
                mIntent.setClass(getActivity(), SearchResumeActivity.class);
                mBundle.putString("mProfessionSearchId", mProfessionSearchId.toString());
                mBundle.putString("mJobTypeSearchId", mJobTypeSearchId.toString());
                startActivity(mIntent);
                getActivity().overridePendingTransition(R.anim.fade_in, com.jaydenxiao.common.R.anim.fade_out);
                break;
            case R.id.job_address_ll: // 工作地点
                break;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
